from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo import settings


def generate_verify_email_url(user_id):
    # serializer = Serializer(秘钥， 有效期：秒)
    serializer = Serializer(settings.SECRET_KEY, 3600 * 24 * 2)
    token = serializer.dumps({'user_id': user_id})
    token = token.decode()

    return token


def check_verify_email_url(token):
    # serializer = Serializer(秘钥， 有效期：秒)
    serializer = Serializer(settings.SECRET_KEY, 3600 * 24 * 2)
    user_id_token = serializer.loads(token)
    # print('----------', user_id_token)
    user_id = user_id_token.get("user_id")
    return user_id


if __name__ == '__main__':
    token = generate_verify_email_url(1)
    check_token = check_verify_email_url(token)
    print(token)
    print(check_token)
